package com.zw.yzk.kabu.data.repository;

import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.GoodsOrderEntity;
import com.zw.yzk.kabu.data.network.service.GoodsOrderService;
import com.zw.yzk.kabu.data.network.task.GoodsOrderTask;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2017/1/4.
 */

public class GoodsOrderRepository extends CommonRepository {

    private int orderType;
    private int totalCount;
    private int unpaiedCount;
    private int undelivedCount;
    private int unReceiveCount;
    private int completeCount;
    private int backCount;

    @Inject
    public GoodsOrderRepository() {
        totalCount = GoodsOrderTask.DEFAULT_ORDER_COUNT_PER_PAGE;
        unpaiedCount = GoodsOrderTask.DEFAULT_ORDER_COUNT_PER_PAGE;
        undelivedCount = GoodsOrderTask.DEFAULT_ORDER_COUNT_PER_PAGE;
        unReceiveCount = GoodsOrderTask.DEFAULT_ORDER_COUNT_PER_PAGE;
        completeCount = GoodsOrderTask.DEFAULT_ORDER_COUNT_PER_PAGE;
        backCount = GoodsOrderTask.DEFAULT_ORDER_COUNT_PER_PAGE;
    }

    @Override
    protected Observable connectServer(Task task) {
        GoodsOrderTask orderTask = (GoodsOrderTask) task;

        orderType = orderTask.type;
        switch (orderTask.type) {
            case GoodsOrderTask.GOODS_ORDER_ALL://全部订单
                if (orderTask.start >= totalCount) {
                    return getEmptyList(totalCount);
                } else {
                    return getOrderList(orderTask);
                }
            case GoodsOrderTask.GOODS_ORDER_UNPAIED://待付款
                if (orderTask.start >= unpaiedCount) {
                    return getEmptyList(unpaiedCount);
                } else {
                    return getOrderList(orderTask);
                }
            case GoodsOrderTask.GOODS_ORDER_UNDELIVER://待发货
                if (orderTask.start >= undelivedCount) {
                    return getEmptyList(undelivedCount);
                } else {
                    return getOrderList(orderTask);
                }
            case GoodsOrderTask.GOODS_ORDER_UNRECEIVE://待收货
                if (orderTask.start >= unReceiveCount) {
                    return getEmptyList(unReceiveCount);
                } else {
                    return getOrderList(orderTask);
                }
            case GoodsOrderTask.GOODS_ORDER_COMPLETE://已完成
                if (orderTask.start >= completeCount) {
                    return getEmptyList(completeCount);
                } else {
                    return getOrderList(orderTask);
                }
            case GoodsOrderTask.GOODS_ORDER_BACK:
                if(orderTask.start >= backCount) {
                    return getEmptyList(backCount);
                } else {
                    return getOrderList(orderTask);
                }
            default:
                orderType = GoodsOrderTask.GOODS_ORDER_ALL;
                return getOrderList(orderTask);
        }
    }

    @Override
    protected Func1<GoodsOrderEntity, Observable> getServerData() {
        return new Func1<GoodsOrderEntity, Observable>() {
            @Override
            public Observable call(GoodsOrderEntity entity) {
                switch (orderType) {
                    case GoodsOrderTask.GOODS_ORDER_ALL://全部订单
                        totalCount = entity.result.total;
                        break;
                    case GoodsOrderTask.GOODS_ORDER_UNPAIED://待付款
                        unpaiedCount = entity.result.total;
                        break;
                    case GoodsOrderTask.GOODS_ORDER_UNDELIVER://待发货
                        undelivedCount = entity.result.total;
                        break;
                    case GoodsOrderTask.GOODS_ORDER_UNRECEIVE://待收货
                        unReceiveCount = entity.result.total;
                        break;
                    case GoodsOrderTask.GOODS_ORDER_COMPLETE://已完成
                        completeCount = entity.result.total;
                        break;
                    default:
                        totalCount = entity.result.total;
                        break;
                }
                return Observable.just(entity);
            }
        };
    }

    private Observable<GoodsOrderEntity> getOrderList(GoodsOrderTask task) {
        GoodsOrderService service = ServiceGenerator.createService(GoodsOrderService.class);
        return service.getGoodsOrder(task.start, task.size, task.type, task.userId, task.accessToken);
    }

    private Observable<GoodsOrderEntity> getEmptyList(int totalCount) {
        GoodsOrderEntity entity = new GoodsOrderEntity();
        entity.errCode = Constant.RESULT_OK;
        entity.result.total = totalCount;

        return Observable.just(entity);
    }
}
